const Response = require('../utils/response');
const bookService = require('../services/bookService');

class BookController {
  // 获取书籍列表
  async getBookList(req, res) {
    try {
      const books = await bookService.getBookList();
      res.json(Response.success(books));
    } catch (error) {
      res.json(Response.error(error.message));
    }
  }

  // 创建书籍
  async createBook(req, res) {
    try {
      const bookData = {
        ...req.body,
        user_id: req.user.id,  // 从认证中间件获取用户ID
        created_at: new Date(),
        updated_at: new Date()
      }

      const result = await bookService.createBook(bookData)
      res.json(Response.success(result, '创建成功'))
    } catch (error) {
      res.json(Response.error(error.message));
    }
  }

  // 获取我的书籍
  async getMyBooks(req, res) {
    try {
      const books = await bookService.getUserBooks(req.user.id);
      res.json(Response.success(books));
    } catch (error) {
      res.json(Response.error(error.message));
    }
  }

  // 获取我喜欢的书籍
  async getLikedBooks(req, res) {
    try {
      const books = await bookService.getLikedBooks(req.user.id);
      res.json(Response.success(books));
    } catch (error) {
      res.json(Response.error(error.message));
    }
  }

  // 获取指定用户的书籍
  async getUserPosts(req, res) {
    try {
      const books = await bookService.getUserBooks(req.params.userId);
      res.json(Response.success(books));
    } catch (error) {
      res.json(Response.error(error.message));
    }
  }

  // 获取单本书籍
  async getBookById(req, res) {
    try {
      const userId = req.query.userId || null;  // 从查询参数获取userId
      const book = await bookService.getBookById(req.params.id, userId);
      res.json(Response.success(book));
    } catch (error) {
      res.json(Response.error(error.message));
    }
  }

  // 更新书籍
  async updateBook(req, res) {
    try {
      await bookService.updateBook(req.params.id, req.user.id, req.body);
      res.json(Response.success(null, '更新成功'));
    } catch (error) {
      res.json(Response.error(error.message));
    }
  }

  // 删除书籍
  async deleteBook(req, res) {
    try {
      // 判断是否是管理员路由
      const isAdminRoute = req.originalUrl.includes('/admin/');
      if (isAdminRoute) {
        // 管理员删除，不需要传 userId
        await bookService.deleteBook(req.params.id);
      } else {
        // 普通用户删除，需要检查权限
        await bookService.deleteBook(req.params.id, req.user.id);
      }
      res.json(Response.success(null, '删除成功'));
    } catch (error) {
      console.error('Delete book error:', error);
      res.json(Response.error(error.message));
    }
  }

  // 获取评论列表
  async getComments(req, res) {
    try {
      const userId = req.query.userId || null;  // 从查询参数获取userId
      const comments = await bookService.getComments(req.params.id, userId);
      res.json(Response.success(comments));
    } catch (error) {
      res.json(Response.error(error.message));
    }
  }

  // 添加评论
  async addComment(req, res) {
    try {
      const commentId = await bookService.addComment(req.params.id, req.user.id, req.body.content);
      res.json(Response.success({ id: commentId }, '评论成功'));
    } catch (error) {
      res.json(Response.error(error.message));
    }
  }

  // 点赞书籍
  async likeBook(req, res) {
    try {
      await bookService.likeBook(req.params.id, req.user.id);
      res.json(Response.success(null, '点赞成功'));
    } catch (error) {
      res.json(Response.error(error.message));
    }
  }

  // 取消点赞
  async unlikeBook(req, res) {
    try {
      await bookService.unlikeBook(req.params.id, req.user.id);
      res.json(Response.success(null, '已取消点赞'));
    } catch (error) {
      res.json(Response.error(error.message));
    }
  }

  // 评分书籍
  async rateBook(req, res) {
    try {
      await bookService.rateBook(req.params.id, req.user.id, req.body.score);
      res.json(Response.success(null, '评分成功'));
    } catch (error) {
      res.json(Response.error(error.message));
    }
  }

  // 点赞评论
  async likeComment(req, res) {
    try {
      const { id, commentId } = req.params
      const userId = req.user.id

      await bookService.likeComment(id, commentId, userId)
      res.json(Response.success(null, '点赞成功'))
    } catch (error) {
      res.json(Response.error(error.message))
    }
  }

  // 取消点赞评论
  async unlikeComment(req, res) {
    try {
      const { id, commentId } = req.params
      const userId = req.user.id

      await bookService.unlikeComment(id, commentId, userId)
      res.json(Response.success(null, '取消点赞成功'))
    } catch (error) {
      res.json(Response.error(error.message))
    }
  }

  // 上架书籍
  async publishBook(req, res) {
    try {
      const isAdminRoute = req.originalUrl.includes('/admin/');
      if (isAdminRoute) {
        await bookService.publishBook(req.params.id);
      } else {
        await bookService.publishBook(req.params.id, req.user.id);
      }
      res.json(Response.success(null, '上架成功'));
    } catch (error) {
      res.json(Response.error(error.message));
    }
  }

  // 下架书籍
  async unpublishBook(req, res) {
    try {
      const isAdminRoute = req.originalUrl.includes('/admin/');
      if (isAdminRoute) {
        await bookService.unpublishBook(req.params.id);
      } else {
        await bookService.unpublishBook(req.params.id, req.user.id);
      }
      res.json(Response.success(null, '下架成功'));
    } catch (error) {
      res.json(Response.error(error.message));
    }
  }

  // 删除评论
  async deleteComment(req, res) {
    try {
      await bookService.deleteComment(
        req.params.commentId,
        req.user.id
      );
      res.json(Response.success(null, '删除成功'));
    } catch (error) {
      res.json(Response.error(error.message));
    }
  }
}

module.exports = new BookController(); 